﻿using System;
using System.Collections.Generic;



public class SchoolsData : IDatas
{
    public class OrganizationInfo
    {
        public string id;
        public string number;
        public string province;
        public string city;
        public string district;
        public string contacts;
        public string phone;
        public string address;
        public string price_scheme;
        public string sign_time;
        public string create_time;
        public string facilitator_id;
        public string audit_pass_time;
        public string expire_time;


    }

    public class UserInfo
    {
        public string id;
        public string name;
        public string account;
        public string password;
        public string type;
        public string content_id;
    }


    public class Organization
    {
        public OrganizationInfo orgnazation = new OrganizationInfo();
        public UserInfo user = new UserInfo();
    }

 //   private List<Organization> data = new List<Organization>();
    private Dictionary<string, Organization> data = new Dictionary<string, Organization>();

    protected override int Deserialize()
    {
        data.Clear();

        if (string.IsNullOrEmpty(jsonText))
            return -1;

        List<object> l = null;
        l = MiniJSON.Json.Deserialize(jsonText) as List<object>;

        
        foreach( var item in l )
        {
            Organization o = new Organization();

            Dictionary<string, object> dict1 = item as Dictionary<string, object>;
            Dictionary<string, object> org = dict1["organization"] as Dictionary<string, object>;
            OrganizationInfo oi = new OrganizationInfo();
            oi.id = org["id"].ToString();
            oi.number = org["number"].ToString();
            oi.province = org["province"].ToString();
            oi.city = org["city"].ToString();
            oi.district = org["district"].ToString();
            oi.contacts = org["contacts"].ToString();
            oi.phone = org["phone"].ToString();
            oi.address = org["address"].ToString();
            oi.price_scheme = org["price_scheme"].ToString();
            oi.sign_time = org["sign_time"].ToString();
            oi.create_time = org["create_time"] == null ? null : org["create_time"].ToString();
            oi.facilitator_id = org["facilitator_id"] == null ? null : org["facilitator_id"].ToString(); 
            oi.audit_pass_time = org["audit_pass_time"]==null?null: org["audit_pass_time"].ToString();
            oi.expire_time = org["expire_time"]== null ? null : org["expire_time"].ToString();
            o.orgnazation = oi;

            if ( dict1.ContainsKey("uesr") && dict1["uesr"] != null )
            {
                Dictionary<string, object> us = dict1["uesr"] as Dictionary<string, object>;
                UserInfo ui = new UserInfo();
                ui.id = us["id"] == null ? null : us["id"].ToString();
                ui.name = us["name"] == null ? null : us["name"].ToString();
                ui.account = us["account"] == null ? null : us["account"].ToString();
                ui.password = us["password"] == null ? null : us["password"].ToString();
                ui.type = us["type"] == null ? null : us["type"].ToString();
                ui.content_id = us["content_id"] == null ? null : us["content_id"].ToString();
                o.user = ui;
            }
            else
            {
                o.user = null;
            }

            if ( o.user != null && string.IsNullOrEmpty(o.user.name)== false &&  data.ContainsKey(o.user.name) == false)
                data.Add(o.user.name,o);
        }
        

        return 0;
    }

    public List<string> GetAllOrgnazationName()
    {
        List<string> l = new List<string>();
        foreach( var item in data )
        {
            l.Add(item.Value.user.name);
        }

        return l;
    }

    public string GetIDByName(string name)
    {
        if (data.ContainsKey(name))
            return data[name].orgnazation.id;
        else
            return "";
    }

}

